﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace DevelopTools.UI
{
    public class ClickMovePuYiControl : MonoBehaviour
    {
        public GameObject objParent;

        public List<GameObject> objs;
        public List<GameObject> objChilds;
        public ObjMove[] objmoves;

        public static ClickMovePuYiControl instance;

        void Awake()
        {
            instance = this;
        }

        // Use this for initialization
        void Start()
        {
            objs = new List<GameObject>();
            objmoves = new ObjMove[objParent.transform.childCount];
            objChilds = new List<GameObject>();
            for (int i = 0; i < objParent.transform.childCount; ++i)
            {
                objs.Add(objParent.transform.GetChild(i).gameObject);
                objmoves[i] = objParent.transform.GetChild(i).GetComponent<ObjMove>();
                objChilds.Add(objs[i].transform.GetChild(0).gameObject);
            }
        }
        RaycastHit hit; Ray ray;
        // Update is called once per frame
        void Update()
        {
            if (UIStateControl.instance.mStateCarr == UIStateControl.StateCarR.ShowUI)
                return;
            if (InputControl.instance.isClickMouseDown)
            {
                ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out hit, 100))
                {
                    GameObject hitObj = hit.transform.gameObject;
                    for (int i = 0; i < objs.Count; ++i)
                    {
                        if (hitObj == objs[i])
                        {
                            objmoves[i].MoveForward();

                        }
                        else
                        {
                            //if(objChilds.Contains(hitObj))
                            //{
                            objmoves[i].MoveBack();
                            //}
                        }
                    }

                }
            }






        }

        public void MoveAllBack()
        {
            for (int j = 0; j < objs.Count; ++j)
            {
                objmoves[j].SetOriginal();
            }
        }

        public bool RotReady()
        {
            int stayCount = 0;
            for (int i = 0; i < objmoves.Length; ++i)
            {
                if (objmoves[i].mMoveState == MoveState.stayBack)
                {
                    stayCount++;
                }
                else
                {
                    //objmoves[i].MoveBack();
                }
            }
            if (stayCount == objmoves.Length)
            {
                return true;
            }
            return false;
        }
    }
}